package com.md.album.utils

import com.living.filedownloader.bean.TasksManagerModel
import com.md.album.db.SelfAlbumDB
import com.md.datastore.MysaveAlbumDB
import com.md.network.api.Album
import com.md.network.api.Audio
/**
 * @author liyue
 * created 2021/4/10
 * 遗留问题：TasksManagerModel 转 Audio，没有传audio的url，encrypt写死-1，有隐患
 */
object AlbumUtil {

     fun getAlbumId():Int{
        var res = System.currentTimeMillis()% 100000000
        return res.toInt()
    }

    fun convertToAudioList(list : List<TasksManagerModel>?):List<Audio>{
        var result :ArrayList<Audio> = ArrayList()
        list?.let {
            if (list.isNotEmpty()){
                for (model in list){
                    var audio = convertToAudio(model)
                    audio?.let {
                        result.add(audio)
                    }
                }
            }
        }
        return result
    }

    fun convertToAudio(model: TasksManagerModel?):Audio?{
        var result:Audio?=null
        model?.let {
            result = Audio(model.audioId,"","","",false,model.name,model.path)
        }

        return result
    }

    fun convertSelfAlbumDBToSlefAlbum(albumDB: SelfAlbumDB?): Album? {
        var result:Album? = null
        albumDB?.let {
            result = Album(ArrayList(),albumDB.author,albumDB.cover,albumDB.desc,albumDB.id,0,albumDB.name,null)
        }
        return result
    }

    fun convertMySaveAlbumDBToSlefAlbum(albumDB: MysaveAlbumDB?): Album? {
        var result:Album? = null
        albumDB?.let {
            result = Album(ArrayList(),albumDB.author,albumDB.cover,albumDB.desc,albumDB.id,0,albumDB.name,null)
        }
        return result
    }

    fun  fillAudioData(album: Album, audios: List<Audio>){
        audios.let {
            for (item in it){
                item.albumId = album.id
                item.albumName = album.name
                item.author = if (album.author == null) "未知作者" else album.author
            }
        }
    }
}